package com.mango.repository;


import com.mango.facade.param.Category;
import com.mango.repository.convert.Convert;
import com.mango.repository.mapper.AmazonCategoryMapper;
import com.mango.repository.model.AmazonCategory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class CategoryRepository {

    @Autowired
    private AmazonCategoryMapper amazonCategoryMapper;


    /**
     * @param categoryParentId
     * @return
     */
    public List<Category> getCategoryList(String categoryParentId) {
        List<AmazonCategory> amazonCategories = null;
        if (StringUtils.isBlank(categoryParentId)) {
            amazonCategories = amazonCategoryMapper.selectByIsTotalNode();
        } else {
            AmazonCategory amazonCategory = amazonCategoryMapper.selectByPrimaryKey(categoryParentId);
            if (!"1".equals(amazonCategory.getHasChildren())) {
                return null;
            }
            String[] split = amazonCategory.getChildNodes().split(",");
            amazonCategories = amazonCategoryMapper.selectCategoryListByParentId(split);
        }

        return amazonCategories.stream().map(Convert::toCategory).collect(Collectors.toList());
    }

    /**
     * 获取全部信息
     * @return
     */
    public List<Category> getCategoryListAll() {
        List<AmazonCategory> categoryList = amazonCategoryMapper.selectAll();
        return categoryList.stream().map(Convert::toCategory).collect(Collectors.toList());
    }
}
